<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\ProductOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class ProductOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ProductOrder(['user', 'product']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->column('id')->sortable();
            $grid->column('order_no');
            $grid->column('user.name', '会员名');
            $grid->column('user.flag', '会员标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('product.name', '商品名称');
            $grid->column('product.main_image', '商品图')->image('', 50, 50);
//            $grid->column('type','类型')->using([
//                1 => ''
//            ]);
            $grid->column('total_amount');
            $grid->column('price');
            $grid->column('number');
            $grid->column('sku')->display('规格查看')->modal(function ($modal) {
                $modal->title('规格');
                return admin_view('array', ['data' => $this->sku]);
            });
            $grid->column('method', '配送方式')->using([
                1 => '快递配送',
                2 => '同城配送',
            ]);
            $grid->column('status')->using([
                1 => '待付款', 2 => '已付款', 3 => '待发货', 4 => '已发货',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user.telephone', '会员电话');
                $filter->equal('user.flag', '会员标签');
                $filter->equal('product.name', '商品名称');
                $filter->like('order_no');
                $filter->equal('method', '配送方法')->select([
                    1 => '快递发货',
                    2 => '同城配送',
                ]);
                $filter->equal('status', '订单状态')->select([
                    1 => '待付款', 2 => '已付款', 3 => '待发货', 4 => '已发货',
                ]);
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ProductOrder(), function (Show $show) {
            $show->field('id');
            $show->field('order_no');
            $show->field('user_id');
            $show->field('product_id');
            $show->field('type');
            $show->field('total_amount');
            $show->field('price');
            $show->field('number');
            $show->field('sku');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ProductOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_no');
            $form->text('user_id');
            $form->text('product_id');
            $form->text('type');
            $form->text('total_amount');
            $form->text('price');
            $form->text('number');
            $form->text('sku');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
